package lambda.function;

import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.*;

/**
 * @author jiangwz
 * @create 2021/9/26.
 */
public class FunctionTest {

    @Test
    public void testConsumer() {
        Consumer<String> c = System.out::println;
        c.accept("hello, consumer");
    }

    @Test
    public void testBiConsumer() {
        Map<String, Integer> map = Maps.newHashMap();
        // BiConsumer
        map.forEach((key, value) -> {

        });
    }

    @Test
    public void testBiFunction() {
        // 比Function多一个入参
        BiFunction<Integer, Long, String> biFunction = (var1, var2) -> String.valueOf(var1 + var2);
        System.out.println(biFunction.apply(1, 2L));


        //子类实现有 BinaryOperator  public interface BinaryOperator<T> extends BiFunction<T,T,T>
        // 例如 Collectors.toMap 的 mergeFunction 就是  BinaryOperator<T>  入参同类型,出参也是
        /**
         * @see java.util.function.BinaryOperator
         */
//        Collectors.toMap(Function<? super T, ? extends K> keyMapper,
//                Function<? super T, ? extends U> valueMapper,
//                BinaryOperator<U> mergeFunction)

    }

    @Test
    public void testPredicate() {
        Predicate<Integer> predicate = Predicate.isEqual(0);
        System.out.println(predicate.test(1));
        System.out.println(predicate.test(0));
    }

    @Test
    public void testBiPredicate() {
        BiPredicate<String, List<String>> biPredicate = (s, strings) -> strings.contains(s);
        System.out.println(biPredicate.test("11", Arrays.asList("!1", "11")));
    }

    @Test
    public void testDoubleBinaryOperator(){
        DoubleBinaryOperator doubleBinaryOperator = (left, right) -> left * right;
        System.out.println(doubleBinaryOperator.applyAsDouble(3, 5));
    }

    @Test
    public void testSupplier(){
        Supplier<Map<String, Integer>> supplier = HashMap::new;
        System.out.println(supplier.get());
    }
}
